package com.shop2cn.iapi.agentproducts.testcase;

import java.util.Arrays;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.BatchSetOffshelfBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.service.BatchSetOffshelfCall;
import com.ymatou.iapi.prodstock.parameter.AgentProductPo;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.AgentUsers;
/**
*批量上架
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_BatchSetOffshelf{
	private static BatchSetOffshelfBean BatchSetOffshelfBean;
	private static BatchSetOffshelfCall BatchSetOffshelfCall;
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
	//渠道商
		private static int rootUserId = AgentUsers.rootUserId;
		private static String rootUserName = "NickautoTest";
		//渠道商->代理商1
		private static int agentUserId1 = AgentUsers.agentUserId1;
		private static String agentUserName1 = "NickautoTest";
		//渠道商->代理商2
		private static int agentUserId2 = AgentUsers.agentUserId2;
		private static String agentUserName2 = "NickautoTest";
		//vip渠道
		private static int vipRootUserId = AgentUsers.vipRootUserId;
		private static String vipRootUserName = "NickautoTest";
		//vip渠道->代理
		private static int vipAgentUserId1 = AgentUsers.vipAgentUserId1;
		private static String vipAgentUserName1 = "dgsq408897016";
		//vip渠道->代理->代理
		private static int vipAgentUserId2 = AgentUsers.vipAgentUserId2;
		private static String vipAgentUserName2 = "NickautoTest";
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量上架");
	}

	@Before
	public void caseUp() {
		BatchSetOffshelfBean=new BatchSetOffshelfBean();
		BatchSetOffshelfCall=new BatchSetOffshelfCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(BatchSetOffshelfCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetOffshelf_01() {
		Logger.start(true,"自采商品，商品下架");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			BatchSetOffshelfBean.setProductIdList(Arrays.asList(productId));
			BatchSetOffshelfBean.setUserId(rootUserId);
			BatchSetOffshelfCall.setData(BatchSetOffshelfBean);
			BatchSetOffshelfCall.callService();			
			Logger.verifyEquals("200",BatchSetOffshelfCall.getString("code"),"验证code");
			
			AgentProductPo agentProductPo = ymtProductsIWapper.selectAgentProductByProductId(productId);
			//验证商品状态为下架状态
			Logger.verifyEquals(2,agentProductPo.getStatus(),"验证status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetOffshelf_02() {
		Logger.start(true,"下架商品，商品不存在");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			BatchSetOffshelfBean.setProductIdList(Arrays.asList(productId));
			BatchSetOffshelfBean.setUserId(11);
			BatchSetOffshelfCall.setData(BatchSetOffshelfBean);
			BatchSetOffshelfCall.callService();
			Logger.verifyEquals("200",BatchSetOffshelfCall.getString("code"),"验证code");
			JsonObject productMsgList = BatchSetOffshelfCall.getJsonObject().get("data").getAsJsonObject().get("productMsgList").getAsJsonArray().get(0).getAsJsonObject();
			Logger.verifyEquals(productId, productMsgList.get("productName").getAsString(), "商品上架失败");
			Logger.verifyEquals("商品已被删除", productMsgList.get("msg").getAsString(), "商品上架失败");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetOffshelf_03() {
		Logger.start(true,"下架商品，用户不存在");
		try {
			BatchSetOffshelfBean.setProductIdList(Arrays.asList("123"));
			BatchSetOffshelfBean.setUserId(rootUserId);
			BatchSetOffshelfCall.setData(BatchSetOffshelfBean);
			BatchSetOffshelfCall.callService();
			Logger.verifyEquals("200",BatchSetOffshelfCall.getString("code"),"验证code");
			JsonObject productMsgList = BatchSetOffshelfCall.getJsonObject().get("data").getAsJsonObject().get("productMsgList").getAsJsonArray().get(0).getAsJsonObject();
			Logger.verifyEquals("123", productMsgList.get("productName").getAsString(), "商品上架失败");
			Logger.verifyEquals("商品已被删除", productMsgList.get("msg").getAsString(), "商品上架失败");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetOffshelf_04() {
		Logger.start(true,"代理商品，商品下架，没有权限");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(agentUserId1,agentUserName1,productId,rootUserId,false);
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			BatchSetOffshelfBean.setProductIdList(Arrays.asList(agentProductId));
			BatchSetOffshelfBean.setUserId(agentUserId1);
			BatchSetOffshelfCall.setData(BatchSetOffshelfBean);
			BatchSetOffshelfCall.callService();
			Logger.verifyEquals("200",BatchSetOffshelfCall.getString("code"),"验证code");
			JsonObject productMsgList = BatchSetOffshelfCall.getJsonObject().get("data").getAsJsonObject().get("productMsgList").getAsJsonArray().get(0).getAsJsonObject();
			Logger.verifyEquals(agentProductId, productMsgList.get("productName").getAsString(), "商品上架失败");
			Logger.verifyEquals("代理商品不可操作上下架", productMsgList.get("msg").getAsString(), "商品上架失败");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetOffshelf_05() {
		Logger.start(true,"vip渠道->vip代理->vip代理，vip渠道商品下架，下级vip代理商品状态不变");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(vipAgentUserId1,vipAgentUserName1,productId,vipRootUserId,true);
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			ImportAgentProductBean importAgentProductBean1 = AddAgentProductCallService.initImportAgentProduct(vipAgentUserId2,vipAgentUserName2,agentProductId,vipAgentUserId1,false);
			String agentProductId1 = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean1);

			BatchSetOffshelfBean.setProductIdList(Arrays.asList(productId));
			BatchSetOffshelfBean.setUserId(vipRootUserId);
			BatchSetOffshelfCall.setData(BatchSetOffshelfBean);
			BatchSetOffshelfCall.callService();
			Logger.verifyEquals("200",BatchSetOffshelfCall.getString("code"),"验证code");
			
			AgentProductPo agentProductPo = ymtProductsIWapper.selectAgentProductByProductId(productId);
			//验证渠道商
			Logger.verifyEquals(2,agentProductPo.getStatus(),"验证status");
//			AgentProductPo agentProductPo = ymtProductsIWapper.selectAgentProductByProductId(productId);
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	



}